package com.usian.controller;

import com.usian.feign.CarServerFeign;
import com.usian.feign.OrderServiceFeign;
import com.usian.pojo.OrderInfo;
import com.usian.pojo.TbItem;
import com.usian.pojo.TbOrder;
import com.usian.pojo.TbOrderShipping;
import com.usian.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author chen
 * @date 2021/1/27 - 22:17 - 星期三
 * @project usian_parent
 * @package com.usian.controller
 * @by GGbomd猪猪侠
 */
/*
订单服务
 */
@RestController
@RequestMapping("/frontend/order")
public class OrderControllerWeb {

    @Autowired
    private CarServerFeign carServerFeign;

    @Autowired
    private OrderServiceFeign orderServiceFeign;

    @RequestMapping("/goSettlement")
    public Result goSettlement(String[] ids,String userId){
        // 获取购物车
        Map<String, TbItem> map = carServerFeign.selectCartByUserId(userId);
        // 从购物车获取选中的商品
        List<TbItem> tbItemList = new ArrayList<>();
        // 取到每个商品的id
        for (String id : ids) {
            tbItemList.add(map.get(id));
        }
        if (tbItemList.size() > 0){
            return Result.ok(tbItemList);
        }
        return Result.error("error");
    }

    /**
     * 提交叮当创建订单
     * @param orderItem
     * @param tbOrder
     * @param tbOrderShipping
     * @return
     */
    @RequestMapping("/insertOrder")
    public Result insertOrder(String orderItem, TbOrder tbOrder , TbOrderShipping
            tbOrderShipping){
        //因为一个request中只包含一个request_body.所以feign不支持多个@RequestBody。
        //这里可以用大实体类来接受或者是map也可以
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderItem(orderItem);
        orderInfo.setTbOrder(tbOrder);
        orderInfo.setTbOrderShipping(tbOrderShipping);
        //返回一个订单id
        String orderId = orderServiceFeign.insertOrder(orderInfo);
        if (orderId != null){
            return Result.ok(orderId);
        }
        return Result.error("error");
    }
}
